# Maintainer: David Runge <dvzrv@archlinux.org>
# Maintainer: Daniel M. Capella <polyzen@archlinux.org>
# Contributor: eolianoe <eolianoe [at] googlemail [dot] com>
# Contributor: Hugo Osvaldo Barrera <hugo@barrera.io>
# Contributor: Thomas Weißschuh <thomas t-8ch de>
# Contributor: Étienne Deparis <etienne [at] depar [dot] is>
# Contributor: Max Gautier <mg@max.gautier.name>

pkgname=khal
pkgver=0.11.2
pkgrel=2
pkgdesc='CLI calendar application built around CalDAV'
arch=('any')
url="https://lostpackets.de/khal/"
license=('MIT')
depends=(
  'python'
  'python-atomicwrites'
  'python-click'
  'python-click-log'
  'python-configobj'
  'python-dateutil'
  'python-icalendar'
  'python-pytz'
  'python-pyxdg'
  'python-tzlocal'
  'python-urwid'
)
makedepends=(
  'python-build'
  'python-installer'
  'python-setuptools-scm'
  'python-sphinxcontrib-newsfeed'
  'python-wheel'
)
checkdepends=(
  'python-pytest'
  'python-freezegun'
  'python-hypothesis'
  'python-packaging'
  'vdirsyncer'
)
optdepends=(
  'gawk: for mutt2khal'
  'python-setproctitle: to set process name'
  'vdirsyncer: to synchronize CalDAV calendars'
)
source=($pkgname-$pkgver.tar.gz::https://github.com/pimutils/$pkgname/archive/refs/tags/v$pkgver.tar.gz)
sha256sums=('db28d21f0aee14982b1394f73c0f12b54cfd09f44d577c1f5b37c038ff568463')
b2sums=('06de876159fe6b8c70062e5ca09983f862f1fee44e33f01af86861f061a46dcbdba93bcfd88d1f8f9b3d1a38d111f8989f585fa461a917f186804c333a8ff7cb')

build() {
  cd $pkgname-$pkgver
  SETUPTOOLS_SCM_PRETEND_VERSION="$pkgver" python -m build --wheel --skip-dependency-check --no-isolation
  make -C doc man PYTHONPATH="$PWD"
}

check() {
  cd $pkgname-$pkgver
  env
  export PYTHONPATH="build:$PYTHONPATH"
  export LC_ALL="C"
  pytest -v
}

package() {
  local site_packages=$(python -c "import site; print(site.getsitepackages()[0])")

  cd $pkgname-$pkgver
  python -m installer --destdir="$pkgdir" dist/*.whl
  # executables
  install -vDm 755 bin/{i,}"$pkgname" misc/mutt2khal -t "$pkgdir/usr/bin/"
  # configuration
  install -vDm 644 "$pkgname.conf.sample" "$pkgdir/usr/share/doc/$pkgname/examples/$pkgname.conf"
  # man page
  install -vDm 644 doc/build/man/${pkgname}.1 -t "$pkgdir/usr/share/man/man1/"
  # completions
  install -vd "$pkgdir/usr/share/"{bash-completion/completions,fish/vendor_completions.d,zsh/site-functions}
  _KHAL_COMPLETE=bash_source "$pkgdir"/usr/bin/khal > "$pkgdir/usr/share/bash-completion/completions/$pkgname"
  _KHAL_COMPLETE=fish_source "$pkgdir"/usr/bin/khal > "$pkgdir/usr/share/fish/vendor_completions.d/$pkgname.fish"
  _KHAL_COMPLETE=zsh_source "$pkgdir"/usr/bin/khal > "$pkgdir/usr/share/zsh/site-functions/_$pkgname"
  # desktop file
  install -vDm 644 misc/khal.desktop -t "$pkgdir/usr/share/applications/"
  # docs
  install -vDm 644 {AUTHORS.txt,{CHANGELOG,CONTRIBUTING,README}.rst} -t "$pkgdir/usr/share/doc/$pkgname/"
  # symlink license file
  install -d "$pkgdir"/usr/share/licenses/$pkgname
  ln -s "$site_packages"/${pkgname}-${pkgver}.dist-info/COPYING "$pkgdir"/usr/share/licenses/$pkgname/COPYING
}
